package org.corey.wyp.manager
{
	import flash.events.EventDispatcher;
	
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.effects.Fade;
	import mx.effects.Parallel;
	
	public class LayoutManagerBase extends EventDispatcher implements ILayoutManagerBase
	{
		public var self:UIComponent;
		public var parent:Container;
		protected var _parallel:Parallel;
		
		public function LayoutManagerBase(parent:Container,self:UIComponent)
		{
			this.self=self;
			this.parent=parent;
		}
		
		public function applyLayout(animate:Boolean):void{
			
			var removeUI:UIComponent=parent.getChildren()[0];
			var addUI:UIComponent=self;
			
			var removeFade:Fade=new Fade();
			removeFade.target=removeUI;
			removeFade.alphaTo=0;
			removeFade.duration=2000;
			removeFade.play();
			
			parent.removeChild(removeUI);
			parent.addChild(addUI);
			
			var addFade:Fade=new Fade();
			addFade.target=addUI;
			addFade.alphaTo=1;
			addFade.alphaFrom=0;
			addFade.duration=1000;
			addFade.play();
			
//			var stateNew:State=new State();
//			var optRemove:RemoveChild=new RemoveChild();
//			optRemove.target=parent.getChildren()[0];
//			
//		
//			var optAdd:AddChild=new AddChild();
//			optAdd.target=self;
//
//			
//			Alert.show(UIComponent(optRemove.target).name+"\n"+UIComponent(optAdd.target).name);
//			
//			stateNew.overrides=[optRemove,optAdd];
//			stateNew.name="nextState"+new Date().toDateString();
//			
//			parent.states=[stateNew];
//			
//			if(animate){
//				buildAnimate();
//				var tx:Transition=new Transition();
//				tx.effect=_parallel;
//				tx.toState=stateNew.name;
//				tx.fromState="*";
//				parent.transitions=[tx];
//			}
//			
//			parent.currentState=stateNew.name;
		}
		
		public function get transitioning():Boolean{
			 return _parallel==null?false:_parallel.isPlaying;
		}
		
		public function stopTransition():void{
			if(_parallel!=null){
				_parallel.pause();
			}
		}
		
		protected function buildAnimate():void{
			_parallel=new Parallel();
			
			var fade1:Fade=new Fade();
			fade1.alphaTo=0;
			fade1.alphaFrom=1;
			fade1.target=parent.getChildren()[0];
			fade1.duration=2000;
			
			var fade:Fade=new Fade();
			fade.alphaTo=1;
			fade.alphaFrom=0;
			fade.target=self;
			fade.duration=2000;
			
			_parallel.children=[fade1,fade];
		} 
	}
}